package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.TblSensirionLog;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.TblGatewayMapper;
import com.frame.work.service.TblSensirionLogService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static java.lang.Float.parseFloat;

/**
 * 温湿度传感器0FBEA002
 */
@Data
public class WenShiDuChuanGanQi_0FBEA002 extends BaseFrame {

    private Integer switchState;

    public WenShiDuChuanGanQi_0FBEA002(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);
        //处理此设备特有的数据
        this.setSwitchState(-100);
        this.setActions(getActionMap(data));
        //将温度和湿度进行日志保存
        saveTempAndHumidity(data, this.getGatewayMac(), this.getDeviceMac());
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
    }


    /**
     * @Description:获取温湿度设备的电压值
     * @param: data
     * @return: float
     * @author:wjming
     * @Date:2021/1/26 10:21
     */
    private static float getDianya(String data) {
        //电压值
        String dianya1 = StringUtils.substring(data, data.length() - 36, data.length() - 32);
        dianya1 = StringUtils.substring(dianya1, 2, 4) + StringUtils.substring(dianya1, 0, 2);
        float dianya = parseFloat(StringUtils.substring(dianya1, 0, 1) + '.' + StringUtils.substring(dianya1, 1, 4));
        return dianya;
    }

    /**
     * @Description:获取温度
     * @param: data
     * @return: float
     * @author:wjming
     * @Date:2021/1/26 10:33
     */
    private static float getTemp(String data) {
        //温度
        String temp1 = StringUtils.substring(data, data.length() - 24, data.length() - 20);
        temp1 = StringUtils.substring(temp1, 0, 3) + StringUtils.substring(temp1, 3, 4);
        if ("8".equals(StringUtils.substring(temp1, 0, 1))) {
            temp1 = "-" + StringUtils.substring(temp1, 1, temp1.length());
        }
        float temp = parseFloat(StringUtils.substring(temp1, 0, 3) + '.' + StringUtils.substring(temp1, 3, 4));
        return temp;
    }

    /**
     * @Description:截取湿度
     * @param: data
     * @return: float
     * @author:wjming
     * @Date:2021/1/26 10:25
     */
    private static float getHumidity(String data) {
        //湿度
        String humidity1 = StringUtils.substring(data, data.length() - 20, data.length() - 16);
        float humidity = parseFloat(StringUtils.substring(humidity1, 0, 3) + '.' + StringUtils.substring(humidity1, 3, 4));
        return humidity;
    }

    /**
     * @Description:温度报警上限0001 为 10%
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/26 10:28
     */
    private static String getTempAlarmTop(String data) {
        return StringUtils.substring(data, data.length() - 16, data.length() - 12);
    }

    /**
     * @Description:温度报警下限 0100 为1%
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/26 10:28
     */
    private static String tempAlarmGorge(String data) {
        return StringUtils.substring(data, data.length() - 12, data.length() - 8);
    }

    /**
     * @Description:湿度上限
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/26 10:28
     */
    private static String humidityAlarmTop(String data) {
        return StringUtils.substring(data, data.length() - 8, data.length() - 4);
    }

    /**
     * @Description:湿度下限
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/26 10:28
     */
    private static String humidityAlarmGorge(String data) {
        return StringUtils.substring(data, data.length() - 4, data.length());
    }

    /**
     * @Description: 创建设备action对象信息，用于发送mqtt以及设备状态信息保存
     * @param: data
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author:wjming
     * @Date:2021/1/25 12:47
     */
    private static Map<String, Object> getActionMap(String data) {
        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("temp", Float.toString(getTemp(data)));
        actions.put("humidity", Float.toString(getHumidity(data)));
        actions.put("dianya", Float.toString(getDianya(data)));
        actions.put("tempAlarmTop", getTempAlarmTop(data));
        actions.put("tempAlarmGorge", tempAlarmGorge(data));
        actions.put("humidityAlarmTop", humidityAlarmTop(data));
        actions.put("humidityAlarmGorge", humidityAlarmGorge(data));
        return actions;
    }

    /**
     * @Description:保存设备日志
     * @param: data
     * @param: gatewayMac
     * @param: deviceMac
     * @return: void
     * @author:wjming
     * @Date:2021/1/26 10:39
     */
    public static void saveTempAndHumidity(String data, String gatewayMac, String deviceMac) {
        StringRedisTemplate redisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
        if (StringUtils.isEmpty(redisTemplate.opsForValue().get("sensirionLog_" + deviceMac))) {
            float temp = getTemp(data);
            float humidity = getHumidity(data);
            //进行数据库操作
            TblSensirionLogService sensirionLogService = SpringUtil.getBean(TblSensirionLogService.class);

            TblGatewayMapper gatewayMapper = SpringUtil.getBean(TblGatewayMapper.class);
            String userId = gatewayMapper.getUserIdByGatewayMac(gatewayMac);
            TblSensirionLog sensirionLog = TblSensirionLog.builder().gatewayMac(gatewayMac)
                    .deviceMac(deviceMac)
                    .humidity((double) humidity)
                    .temperature((double) temp)
                    .userId(userId).build();
            sensirionLogService.subTableSaving(sensirionLog);
            //张总要求从10分钟改成50秒
            redisTemplate.opsForValue().set("sensirionLog_" + deviceMac, temp + "_" + humidity, 50, TimeUnit.SECONDS);
            // redisTemplate.opsForValue().set("sensirionLog_" + deviceMac, temp + "_" + humidity, 10, TimeUnit.MINUTES);
        }

    }
}
